package com.ruoyi.wx.utils;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.wx.config.WxV3PayConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class WeChatTokenManager {

    private static final Object EXPERT_LOCK = new Object();
    private static String expertAccessToken;
    private static long expertExpireTime;

    private static final Object PROGRAM_USER_LOCK = new Object();
    private static String programUserAccessToken;
    private static long programUserExpireTime;

    private static final Object USER_LOCK = new Object();
    private static String userAccessToken;
    private static long userExpireTime;

    public static String getExpertAccessToken() {
        long now = System.currentTimeMillis();
        synchronized (EXPERT_LOCK) {
            if (expertAccessToken == null || now >= expertExpireTime) {
                String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential"
                        + "&appid=wxbcc5699da872e684"
                        + "&secret=d8a47acf495cd00312dcdac3d32c8174";

                String result = HttpUtil.get(url);
                System.out.println("微信返回原始内容：" + result);

                JSONObject json = JSON.parseObject(result);
                if (json.containsKey("access_token")) {
                    expertAccessToken = json.getString("access_token");
                    int expiresIn = json.getIntValue("expires_in");
                    expertExpireTime = now + (expiresIn - 200) * 1000L;
                    System.out.println("【专家服务号】重新获取 access_token：" + expertAccessToken);
                } else {
                    log.error("获取 access_token 失败：" + json);
                }
            }
            return expertAccessToken;
        }
    }

    public static String getUserAccessToken() {
        long now = System.currentTimeMillis();
        synchronized (USER_LOCK) {
            if (userAccessToken == null || now >= userExpireTime) {
                String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential"
                        + "&appid=wx09149cc3c3dd020c"
                        + "&secret=989f3e1b83ecc70d21c57d35ce040ff8";

                String result = HttpUtil.get(url);
                System.out.println("微信返回原始内容：" + result);
                JSONObject json = JSON.parseObject(result);
                if (json.containsKey("access_token")) {
                    userAccessToken = json.getString("access_token");
                    int expiresIn = json.getIntValue("expires_in");
                    userExpireTime = now + (expiresIn - 200) * 1000L;
                    System.out.println("【用户服务号】重新获取 access_token：" + userAccessToken);
                } else {
                    log.error("获取 access_token 失败：" + json);
                }
            }
            return userAccessToken;
        }
    }

    public static String getProgramUserAccessToken() {
        long now = System.currentTimeMillis();
        synchronized (PROGRAM_USER_LOCK) {
            if (programUserAccessToken == null || now >= programUserExpireTime) {
                String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential"
                        + "&appid=" + WxV3PayConfig.APP_ID
                        + "&secret=" + WxV3PayConfig.secret;

                String result = HttpUtil.get(url);
                System.out.println("微信返回原始内容：" + result);

                JSONObject json = JSON.parseObject(result);
                if (json.containsKey("access_token")) {
                    programUserAccessToken = json.getString("access_token");
                    int expiresIn = json.getIntValue("expires_in");
                    programUserExpireTime = now + (expiresIn - 200) * 1000L;
                    System.out.println("【用户小程序】重新获取 access_token：" + expertAccessToken);
                } else {
                    log.error("获取 access_token 失败：" + json);
                }
            }
            return programUserAccessToken;
        }
    }
}
